/*
 * Ext JS Library 2.2.1
 * Copyright(c) 2006-2009, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
//this is the actual main page that loads a viewport containing all the child elements/panels etc
Application = {};

Ext.onReady(function(){
    Ext.QuickTips.init();

   
   

    var tpl = Ext.Template.from('preview-tpl', {
        compiled:true,
        getBody : function(v, all){
            return Ext.util.Format.stripScripts(v || all.description);
        }
    });
    Application.getTemplate = function(){
        return tpl;
    }

    var treePanel = new TreePanel();  //saved searches
    var tabContainer = new TabContainerPanel();
 //   tabContainer.add(tabContainer.QuarantineTab);

    treePanel.on('feedselect', function(feed){
        tabContainer.FeedGrid.loadFeed(feed);
    });
    
    //Create the required tabs and add them to the tabcontainerpanel
    //we need: grid style quarantine tab for files that have been deemed failures
    //we need: grid style manualrelease tab for files that have passed but need someone to approve the release //do last
    
    
    
    
    var viewport = new Ext.Viewport({
        layout:'border',
        items:[
            new Ext.BoxComponent({ // raw element
                region:'north',
                el: 'header',
                height:32
            }),
            treePanel,
            tabContainer
         ]
    });

    // add the default log searchers


    treePanel.addTorrentRelease({
        url:'http://rss.thepiratebay.org/201',
        text: 'PirateBay - Movies'
    }, true);
    
       treePanel.addTorrentRelease({
        url:'http://rss.thepiratebay.org/205',
        text: 'PirateBay - Tv Shows'
    }, true);
        treePanel.addTorrentRelease({
        url:'http://www.mininova.org/rss.xml?user=axxo&direct',
        text: 'Axxo releases'
    }, true);
   
});

var task = {
    run: function(){
        Ext.getCmp('torrents-panel').store.load({params: {action: 'listTorrents'}});
    },
    interval: 1500 //1 second
}
var runner = new Ext.util.TaskRunner();
runner.start(task);

// This is a custom event handler passed to preview panels so link open in a new windw
Application.LinkInterceptor = {
    render: function(p){
        p.body.on({
            'mousedown': function(e, t){ // try to intercept the easy way
                t.target = '_blank';
            },
            'click': function(e, t){ // if they tab + enter a link, need to do it old fashioned way
                if(String(t.target).toLowerCase() != '_blank'){
                    e.stopEvent();
                    window.open(t.href);
                }
            },
            delegate:'a'
        });
    }
};